package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.entity.vo.ecology.AdminUserEcologyCastUpperLimitDetailsVO;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.UserEcologyCastUpperLimit;
import com.sc.nft.mapper.UserEcologyCastUpperLimitMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 生态-用户铸造上限表(sc_user_ecology_cast_upper_limit)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-03-12 20:04:34
 */
@Slf4j
@Repository
public class UserEcologyCastUpperLimitDao extends ServiceImpl<UserEcologyCastUpperLimitMapper, UserEcologyCastUpperLimit> {

    public UserEcologyCastUpperLimit getOneByUserIdAndCollectionId(Long userId, Long collectionId) {
        LambdaQueryWrapper<UserEcologyCastUpperLimit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEcologyCastUpperLimit::getUserId, userId);
        wrapper.eq(UserEcologyCastUpperLimit::getCollectionId, collectionId);
        wrapper.last("order by id  desc limit 1");
        return getOne(wrapper);
    }

    public List<AdminUserEcologyCastUpperLimitDetailsVO> userEcologyCastUpperLimitDetails(String userTel) {
        return baseMapper.userEcologyCastUpperLimitDetails(userTel);
    }

    public Boolean addCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count) {
        LambdaUpdateWrapper<UserEcologyCastUpperLimit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyCastUpperLimit::getUserId, userId);
        wrapper.eq(UserEcologyCastUpperLimit::getCollectionId, collectionId);
        wrapper.setSql("usable_count = usable_count + " + count);
        return update(wrapper);
    }

    public Boolean addNewCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, Integer usableCount) {
        LambdaUpdateWrapper<UserEcologyCastUpperLimit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserEcologyCastUpperLimit::getUserId, userId);
        wrapper.eq(UserEcologyCastUpperLimit::getCollectionId, collectionId);
        wrapper.eq(UserEcologyCastUpperLimit::getUsableCount,usableCount);
        wrapper.setSql("usable_count = usable_count + " + count);
        return update(wrapper);
    }

    public Boolean subExchangeCount(Long userId, Long collectionId, Integer count) {
        return baseMapper.subExchangeCount(userId, collectionId, count);
    }

    public boolean subCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count) {
        return baseMapper.subCountByUserIdAndCollectionId(userId, collectionId, count);
    }

    public boolean subNewCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, Integer usableCount) {
        if (count.intValue() > usableCount){
            count = usableCount;
        }
        return baseMapper.subNewCountByUserIdAndCollectionId(userId, collectionId, count, usableCount);
    }
}